package com.xiaoma.weakreference;

import java.lang.ref.WeakReference;

/**
 * https://www.cnblogs.com/zjj1996/p/9140385.html
 * @author xiaomacdut
 * @date 2020年2月15日 下午2:41:57
 */
public class TestWeakReference{
    
    public static void main(String [] args){
        
        Car car = new Car(22000, "silver");
        WeakReference<Car> weakCar = new WeakReference<Car>(car);
        int i = 0;
        //
        while(true){
            // System.out.println("here is the strong reference 'car' " + car);
            if(weakCar.get() != null){
                i++;
                System.out.println("Object is alive for " + i + " loops - " + weakCar);
            }else{
                System.out.println("Object has been collected.");
                break;
            }
        }
        
    }
    
    /*
    public static void main(String [] args){
        
        Car car = new Car(22000, "silver");
        WeakReference<Car> weakCar = new WeakReference<Car>(car);
        
        int i = 0;
        
        while(true){
            System.out.println("here is the strong reference 'car' " + car);
            if(weakCar.get() != null){
                i++;
                System.out.println("Object is alive for " + i + " loops - " + weakCar);
            }else{
                System.out.println("Object has been collected.");
                break;
            }
        }
    }*/
}
